(function() {
	
	if (!window.CJ) {
		window.CJ = {};
	}

	function Channel () {
		this.name = 'Channel';
		this.rootID = '#ch';
		this.rootElem = $(this.rootID+" ul");
		this.moveDistance = -88;
		this.pageUnitCount = 4;
		this.bindEvent();
		this.searchProgramID = null;			// 2013.08.13 by SINZINET
	}

	Channel.lists = [];
	Channel.totalCount = 0;
	Channel.pageSize = 50;
	Channel.pageNo = 0;
	Channel.setID = function (id) {
		this.lastID = id;
	};
	Channel.isDataLoading = false;
	Channel.getData = function (callback, errorCB, completeCB) {
		if (Channel.isDataLoading ) {
			console.log('Channel data loading....');
			return;
		}
		Channel.isDataLoading = true;
		CJ.HttpJson.get({
			type: 'CHANNELLIST',
			param: {
				pageSize: this.pageSize,
				pageNo: this.pageNo+1
			},
			success: function (json) {
				Channel.successCB(json, callback);
			},
			error: errorCB,
			complete: function () {
				if (!!completeCB && typeof completeCB == 'function')		// 2013.08.13 by SINZINET
					completeCB();
				Channel.isDataLoading = false;
			}
		});
	};
	Channel.getMore = function () {
		if (this.totalPage != this.pageNo) {
			this.getData();
		}
	};
	Channel.successCB = function (json, callback) {
		this.lists  = this.lists.concat(json.channellist);
		this.totalCount = parseInt(json.total_count);
		this.totalPage =  parseInt(json.page_info.total_page);
		this.pageNo =  parseInt(json.page_info.page);
		CJ.Scene["Channel"].render(json.channellist);
		CJ.Scene["ChannelSub"].render(json.channellist);
		if (typeof callback == 'function') {
			callback();
		}
	};

	Channel.prototype = new CJ.Vertical();
	Channel.prototype.bindEvent = function (lists) {
		//hover
		$('#ch .scroll ul').on('mouseenter', 'li', function() {
			$('#global li.focus').removeClass('focus');
			CJ.Scene.setDelegate(CJ.Scene["Channel"]);
		}).on('mouseout', 'li', function(evt){
		});
		CJ.Vertical.prototype.bindEvent.call(this);
	};
	Channel.prototype.render = function (lists) {
		var childCount = this.rootElem.children().length;
		var method =  childCount > 0 ? 'append' : 'html';
		$( this.rootElem )[method](
			$.render.channelTemplate( lists )
		)
		if (method == 'html') {
			var initIndex = lists.length > 4 ? 3 : 0;
			var topPX = parseInt(initIndex * this.moveDistance )+ 'px';
			$( this.rootElem ).css({top: topPX}).children('li:nth-child('+parseInt(initIndex+1)+')').addClass('select focus');
		}
	};
	Channel.prototype.extend = function() {
		Channel.getMore();
	};
	Channel.prototype.setFocus = function (cID) {
		var initIndex = $( 'li[data-channelid="'+cID+'"]', this.rootElem ).index();
		var topPX = parseInt(initIndex * this.moveDistance) + 'px';
		$( this.rootElem ).css({top: topPX}).children('li:nth-child('+parseInt(initIndex+1)+')').addClass('select focus').siblings().removeClass('select focus');
	};
	Channel.prototype.right = function () {
		//우측 상단 메뉴로 이동 TBD
		CJ.Scene.myinfo();
	};
	Channel.prototype.enter = function (evt) {
		var now = $("li.select", this.rootElem);
		var cID = now.data("channelid");
		CJ.Program.getData(cID);
	};

	// 2013.08.13 by SINZINET
	Channel.prototype.back = function () {
		if (!!this.searchProgramID) {
			window.NetCastBack();
		}
	};
	
	CJ.Channel = Channel;


	function ChannelSub () {
		this.name = 'ChannelSub';
		this.template = 'channelSubTemplate';
		this.rootID = '#vod div.list .dep1';
		this.did = 'data-channelid';
		this.rootElem = $(this.rootID+" ol");
		this.bindEvent();
	}
	ChannelSub.prototype = new CJ.SubList();
	ChannelSub.prototype.enter = function (evt) {
		this.extend();
		var now = $(this.rootID + ' .focus');
		if (now.hasClass('btn_prev')){
			CJ.Horizon.prototype.pageUp.call(this);
		} else if (now.hasClass('select')) {
			//컨텐츠 선택
			cID = now.data('channelid');
			CJ.Program.getData(cID, true);
			CJ.Scene["Channel"].setFocus(cID);
			CJ.Scene.sub('ProgramSub');
		} else if (now.hasClass('btn_next')){
			CJ.Horizon.prototype.pageDown.call(this);
		}
	};
	CJ.ChannelSub = ChannelSub;
		
	$.extend(CJ.Scene, {
							channel: function(){
								$(this["Channel"].rootID + " li.select").addClass('focus');
								$("#wrap").removeClass().addClass(this["Channel"].rootID.replace('#',''));
								this.setDelegate(this["Channel"]);
							}
						});


	$(document).ready(function(){

		CJ.Scene["Channel"] = new CJ.Channel();
		CJ.Scene["ChannelSub"] = new CJ.ChannelSub();

	});

})();